import java.io.FileNotFoundException;
import animationEngine.InputFrame;
import animationEngine.MainFrame;

/**
 * The Class Main.  This is where the program is run from and interactions between the windows.
 * 
 * @author Andrei Palade
 * @author David McBrierty
 * @author Garry Sharp
 * @author Hristo Georgiev
 * @author John Dennis
 * @author Marco Sarconi
 * @version 1.0.1
 */
public class Main {

	/** The START time of the program */
	private static float START_TIME = System.nanoTime();
	
	/** The previous text. Used for creating a new pattern from a currently running window. */
	public static String previousText ;
	
	/** The previous pattern. Used for creating a new pattern from a currently running window.*/
	public static String previousPattern;
	
	/** The previous algorithm. Used for creating a new pattern from a currently running window.*/
	public static String previousAlgorithm;

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 * @throws FileNotFoundException the file not found exception
	 * @throws InterruptedException the interrupted exception
	 */
	public static void main(String[] args) throws FileNotFoundException, InterruptedException {



		// This is the entry point of the program.
		@SuppressWarnings("unused")
		boolean keepFrame = false;
		
		while(true){
		
			InputFrame startFrame = null;
		
			String textFromTextField = null;// = startFrame.getInsertTextField().getText();
			String textFromPatternField = null;// = startFrame.getInsertPatternField().getText();
			
			// Add the previous pattern and text to the input frame if they exist
			startFrame = (previousPattern != null && previousText != null)?
					new InputFrame(previousText,previousPattern,null): new InputFrame();
					
			startFrame.setLocationRelativeTo(null);
			
			
			int algorithmChoice = 0;// = startFrame.getAlgorithmsList().getSelectedIndex();
			
			// Until the input is provided
			while(!startFrame.isDone()){
				// Interrupt the rest of the execution
				Thread.sleep(1);
			}
			
			// If the input has been provided, start processing it 
			if(startFrame.isDone()){
				//System.out.println("Start frame is done");
				textFromTextField = startFrame.getInsertTextField().getText();
				textFromPatternField = startFrame.getInsertPatternField().getText();
				algorithmChoice = startFrame.getAlgorithmsList().getSelectedIndex();

				//System.out.println("We have chosen text of:" + textFromTextField);
				//System.out.println("We have chosen pattern of:" + textFromPatternField);
				//System.out.println("We have chosen algorithm of:" + algorithmChoice);
				keepFrame = true;
				startFrame.dispose();

			}
			
			// Update the input frame with the previous inputs
			previousPattern = textFromPatternField;
			previousText = textFromTextField;
			
			// input frame is done - and we have info required to start main frame		  
			String algo;

			if(algorithmChoice == 0){
				algo = "naive";
			}
			else if(algorithmChoice == 2){
				algo = "kmp";
			}
			else{
				algo = "boyer";
			}


			//create the main frame and initialise it
			MainFrame m = new MainFrame(textFromTextField, textFromPatternField, algo);
			m.init();
			
			float timeSinceLastUpdate = (System.nanoTime());
			
			//make it visible
			m.setVisible(true);

			while (true){
				m.update(timeSinceLastUpdate);
				timeSinceLastUpdate = (System.nanoTime()-START_TIME) / 1000000000.0f;
				//timeSinceLastUpdate = (System.currentTimeMillis() - timeSinceLastUpdate);
				
				// The main frame not finished its execution yet
				if (m.isJobDone()){
					break;
				}
			}
			
			// Dispose of the main frame
			m.dispose();
		}
	}

}
